package com.controller;

import com.entity.BusinessUtilityBillRecord;
import com.service.BusinessUtilityBillRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【经营项目】水电费缴费记录
 */

@RestController
@RequestMapping("/api/business/utility/record")
public class BusinessUtilityBillRecordController {

    @Autowired
    private BusinessUtilityBillRecordService businessUtilityBillRecordService;

    @PostMapping
    public BusinessUtilityBillRecord createBusinessUtilityBillRecord(@RequestBody BusinessUtilityBillRecord businessUtilityBillRecord) {
        return businessUtilityBillRecordService.save(businessUtilityBillRecord);
    }

    @DeleteMapping("/{id}")
    public void deleteBusinessUtilityBillRecord(@PathVariable Integer id) {
        businessUtilityBillRecordService.deleteById(id);
    }

    @PutMapping("/{id}")
    public BusinessUtilityBillRecord updateBusinessUtilityBillRecord(@PathVariable Integer id, @RequestBody BusinessUtilityBillRecord businessUtilityBillRecord) {
        businessUtilityBillRecord.setId(id);
        return businessUtilityBillRecordService.update(businessUtilityBillRecord);
    }

    @GetMapping("/{id}")
    public BusinessUtilityBillRecord getBusinessUtilityBillRecordById(@PathVariable Integer id) {
        return businessUtilityBillRecordService.findById(id);
    }

    @GetMapping
    public List<BusinessUtilityBillRecord> getAllBusinessUtilityBillRecords() {
        return businessUtilityBillRecordService.findAll();
    }

    @GetMapping("/page")
    public Page<BusinessUtilityBillRecord> getBusinessUtilityBillRecordsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return businessUtilityBillRecordService.findAll(pageable);
    }
}